from common.config import *
import requests
import allure
from common.log_info import Logger

goods = Logger("goods", "goods").logger


class Goods:

    @allure.step('创建商品')
    def create_goods(self, env, spec_attr_id, spec_attr_option_list, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/create"
        data = {
            "remark": "商品备注测试1",
            "name": "自动化专用测试商品",
            "aliasName": "商品别名测试1",
            "slogan": "口号卖点测试1",
            "brandName": "品牌名称测试1",
            "supplier": "采购厂商测试1",
            "code": "商品编码测试test{0:%Y-%m-%d-%H-%M-%S}".format(datetime.datetime.now()),
            "barCode": "商品条码测试{0:%Y-%m-%d-%H-%M-%S}".format(datetime.datetime.now()),
            "outCode": "外部商品编码测试{0:%Y-%m-%d-%H-%M-%S}".format(datetime.datetime.now()),
            "type": "PHYSICAL",
            "extAttrFlags": {
                "packingUnitFlag": 1,
                "volumeFlag": 2,
                "weightFlag": 0
            },
            "skus": [{
                "code": "skubianma01{0:%Y-%m-%d-%H-%M-%S}".format(datetime.datetime.now()),
                "marketPrice": 100,
                "packingUnit": "台",
                "sellingPrice": 1,
                "volume": 30000,
                "weight": "",
                "mainImgUrl": "",
                "attachImgUrl": "",
                "specification": [{
                    "specAttrId": spec_attr_id,
                    "specAttrOptionId": spec_attr_option_list[0]
                }]
            }, {
                "code": "skubianma02{0:%Y-%m-%d-%H-%M-%S}".format(datetime.datetime.now()),
                "marketPrice": 200,
                "packingUnit": "台",
                "sellingPrice": 2,
                "volume": 50000,
                "weight": "",
                "mainImgUrl": "",
                "attachImgUrl": "",
                "specification": [{
                    "specAttrId": spec_attr_id,
                    "specAttrOptionId": spec_attr_option_list[1]
                }]
            }],
            "specAttrList": [{
                "isMain": False,
                "optionList": [{
                    "specAttrOptionId": spec_attr_option_list[0],
                    "imgUrl": ""
                }, {
                    "specAttrOptionId": spec_attr_option_list[1],
                    "imgUrl": ""
                }],
                "specAttrId": spec_attr_id
            }],
            "otherMediaFlags": {
                "skuImgFlag": 0,
                "spuAttachImgsFlag": 0,
                "spuVideoFlag": 0,
                "skuVideoFlag": 0,
                "mainAttrFlag": 0
            },
            "attachImgUrls": "",
            "videoUrl": "",
            "mainImgUrl": "https://buorgcostest.rongyi.com/member-logo/258/3389112732350800145631792205690608777.jpg",
            "description": "<p><span style=\"color:#6a8759\">商品详情测试1</span></p>"
        }
        goods.info("创建商品的请求参数：{}".format(data))
        return requests.post(json=data, url=url, headers=headers)

    @allure.step('商品列表')
    def goods_list(self, env, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        data = {"current": 1, "size": 10}
        url = zhongtai_url + "platform-goods-server/v1/spu/list"
        goods.info("查看商品列表的请求参数：{}".format(data))
        return requests.post(json=data, url=url, headers=headers)

    @allure.step('删除商品')
    def delete_goods(self, env, goods_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/delete"
        data = {"id": goods_id}
        goods.info("删除商品的请求参数：{}".format(data))
        return requests.post(json=data, url=url, headers=headers)

    @allure.step('商品状态修改（生效；失效）')
    def modify_status(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/status"
        goods.info("修改商品状态的请求参数：{}".format(data))
        return requests.post(json=data, url=url, headers=headers)

    @allure.step('商品sku批量保存价格')
    def modify_sku_price(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/modify/sku/price"
        goods.info("请求参数：{}".format(data))
        return requests.post(json=data, url=url, headers=headers)

    @allure.step('商品里sku列表')
    def sku_list_goods(self, env, spu_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/sku/{}".format(spu_id)
        goods.info("spuid：{}".format(spu_id))
        return requests.get(url=url, headers=headers)

    @allure.step('商品详情查询')
    def query_detail(self, env, spu_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/detail/{}".format(spu_id)
        return requests.get(url=url, headers=headers)

    @allure.step('商品编辑')
    def modify_goods(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/spu/modify"
        goods.info("商品编辑的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('创建类目')
    def create_category(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/create"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('修改类目')
    def update_category(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/update"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('移动类目')
    def move_category(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/move"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('删除类目')
    def delete_category(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/delete"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step("迁移商品")
    def migrate_goods(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/migrate"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('类目树查询')
    def category_tree(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/tree"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格添加')
    def add_spec_attr(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/save"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格编辑')
    def update_spec_attr(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/update"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格组删除')
    def delete_spec_attr(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/delete"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格组列表')
    def spec_attr_list(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/pageList"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格选项列表-tree')
    def spec_attr_option_list(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/option/treeList"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格选项列表--顶层')
    def spec_attr_option_page_list(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/option/pageList"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('规格组添加一个选项值')
    def add_spec_attr_option(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/specAttr/option/add"
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品统计')
    def sale_goods_statistics(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/statistics"
        goods.info("销售商品统计的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品提醒消息列表')
    def sale_goods_remind_list(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/remind/list"
        goods.info("销售商品提醒消息列表的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品提醒消息清空接口')
    def sale_goods_remind_clear(self, env, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/remind/clear"
        return requests.post(url=url, headers=headers)

    @allure.step('销售商品上下架')
    def sale_goods_change_sale_status(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/changeSaleStatus"
        goods.info("销售商品上下架的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品上下架_批量')
    def sale_goods_change_sale_status_batch(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/changeSaleStatus/batch"
        goods.info("商品批量上下架的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品列表')
    def sale_goods_list(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/list"
        goods.info("销售商品列表的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品更改sku库存')
    def sale_goods_update_stock(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/sku/updateStock"
        goods.info("更改商品库存的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('销售商品sku列表接口-根据spu查询spu下的sku列表')
    def sale_goods_spu_sku_list(self, env, spu_id, branch_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + f"platform-goods-server/v1/sale/spu/sku/list?spuId={spu_id}&branchId={branch_id}"
        goods.info("商品的spu_id={}，节点branch_id={}".format(spu_id, branch_id))
        return requests.get(url=url, headers=headers)

    @allure.step('销售商品sku价格变更')
    def sale_goods_sku_update_price(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/spu/sku/updatePrice"
        goods.info("商品价格变更的请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('修改节点开启运营权限')
    def branch_push_batch(self, env, status, branch_id_list, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + f"platform-goods-server/v1/branchPush/{status}"
        data = {"branchIdList": branch_id_list}
        goods.info("修改状态：{},请求参数：{}".format(status, data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step('节点运营权限查看')
    def branch_push_query_manage_status_by_branch_id(self, env, branch_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + f"platform-goods-server/v1/branchPush/queryManageStatusByBranchId?branchId={branch_id}"
        goods.info("节点ID：{}".format(branch_id))
        return requests.get(url=url, headers=headers)

    @allure.step("查询类目是否开启了门店自主经营权限")
    def get_branch_category_sava_self_own_status(self, env, branch_id, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + f"platform-goods-server/v1/category/getSelfOwnStatus?branchId={branch_id}"
        goods.info("查询branch_id：{}".format(branch_id))
        return requests.get(url=url, headers=headers)

    @allure.step("门店类目的自主管理开启与关闭")
    def category_sava_self_own_status(self, env, branch_id, self_own_status, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/category/savaSelfOwnStatus"
        data = {
            "branchId": branch_id,
            "selfOwnStatus": self_own_status
        }
        goods.info("请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)

    @allure.step("sku的rfid维护")
    def sale_sku_rfid(self, env, data, headers):
        zhongtai_url = getconf(env, "zhongtai_url")
        url = zhongtai_url + "platform-goods-server/v1/sale/sku/rfid/save"
        goods.info("请求参数：{}".format(data))
        return requests.post(url=url, json=data, headers=headers)
